package cn.gotom.svg;

import android.graphics.Canvas;
import cn.gotom.svg.xml.StyleAttribute;
import cn.gotom.svg.xml.XMLParseUtil;

/**
 * @author 裴绍国
 * @author <a href="mailto:peixere@gmail.com">裴绍国</a>
 */
public class Polygon extends PathElement
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Polygon()
	{
	}

	protected void build() throws SVGException
	{
		super.build();
	}

	public void render(Canvas canvas) throws SVGException
	{
		if (this.visible())
		{
			beginLayer(canvas);
			renderPolygon(canvas);
			finishLayer(canvas);
		}
	}

	private void renderPolygon(Canvas canvas)
	{
		buildPath();
		doFillPaint();
		canvas.drawPath(path, paint);
		if (doStrokePaint())
		{
			canvas.drawPath(path, paint);
		}
	}

	protected void buildPath()
	{
		this.setFillType();
		StyleAttribute attr = new StyleAttribute();
		if (getAttribute(attr.setName(StyleAttribute.points)))
		{
			String pointsStr = attr.getStringValue();
			float[] points = XMLParseUtil.parseFloatList(pointsStr);
			path.moveTo(points[0], points[1]);
			for (int i = 2; i < points.length; i += 2)
			{
				path.lineTo(points[i], points[i + 1]);
			}
			path.close();
		}
	}

}
